package com.example.myapplication.ui.components


import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.navigation.Destination
import com.example.myapplication.ui.screens.ArticleDetailScreen
import com.example.myapplication.ui.screens.MainFarme
import com.example.myapplication.ui.screens.VideoDetailScreen
import com.google.accompanist.insets.ProvideWindowInsets
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController

/**
 * 导航控制器
 * **/
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun NavHostApp() {
    val navController = rememberAnimatedNavController()
    // 使用Accompanist的navigationBarsPadding需要在外层包裹ProvideWindowInsets
    ProvideWindowInsets() {
        AnimatedNavHost(
            navController = navController,
            startDestination = Destination.HomeFrame.route
        ) {
            // 首页框架
            // enterTransition进入当前页面的的动画
            // exitTransition退出页面的动画
            composable(Destination.HomeFrame.route, enterTransition = {
                // 从左到右
                slideIntoContainer(AnimatedContentScope.SlideDirection.Right)
            }, exitTransition = {
                slideOutOfContainer(AnimatedContentScope.SlideDirection.Left)
            }) {
                MainFarme(onNavigateToArticle = {
                    // 点击文章item触发
                    navController.navigate(Destination.ArticleDetail.route)
                }, onNavigateToVideo = {
                    navController.navigate(Destination.VideoDetail.route)
                })
            }
            // 文章详情
            composable(Destination.ArticleDetail.route, enterTransition = {
                // 从左到右
                slideIntoContainer(AnimatedContentScope.SlideDirection.Right)
            }, exitTransition = {
                slideOutOfContainer(AnimatedContentScope.SlideDirection.Left)
            }) {
                ArticleDetailScreen(onBack = {
                    navController.popBackStack()
                })
            }
            // 视频详情
            composable(Destination.VideoDetail.route, enterTransition = {
                // 从左到右
                slideIntoContainer(AnimatedContentScope.SlideDirection.Right)
            }, exitTransition = {
                slideOutOfContainer(AnimatedContentScope.SlideDirection.Left)
            }) {
                VideoDetailScreen(onBack = {
                    navController.popBackStack()
                })
            }
        }
    }
}


